import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    CategoryData: [],
    ListData: [],
    ShopDetailsData: [],
    DetailsData: {},
    selectIndex: 0,
    CartData: JSON.parse(localStorage.getItem("MTCart")) || [],
    showListData: [],
    totalPrice: 0,
  },
  mutations: {
    GetData(state, GetData) {
      state.CategoryData = GetData.CategoryData.splice(0, 10);
      state.ListData = GetData.ListData;
      
    },
    DetailsId(state, Data) {
      state.ShopDetailsData = Data.ShopDetailsData;
      // state.DetailsData = [];
      [...Data.ShopDetailsData].forEach((item) => {
        // console.log(item.data.mtWmPoiId)
        if (item.data.mtWmPoiId === Data.id) {
          state.DetailsData = { ...item.data };
          // console.log(state.DetailsData);
        }
      });
      state.showListData = state.DetailsData.categoryList[state.selectIndex];
      state.totalPrice = 0;
      [...state.CartData].forEach((elem) => {
        state.totalPrice += elem.count * elem.originPrice;
        // console.log(state.totalPrice);
      });
      localStorage.setItem("MTCart", JSON.stringify(state.CartData));
      // console.log(state.showListData)
      // console.log(state.DetailsData.categoryList[state.selectIndex])
    },
    SelectIndex(state, index) {
      state.selectIndex = index;
      state.showListData = state.DetailsData.categoryList[index];
    },
    AddCart(state, item) {
      // console.log(item);
      var flag = false;
      state.CartData.forEach((elem) => {
        if (item.spuId == elem.spuId) {
          elem.count++;
          flag = true;
        }
      });
      state.showListData.spuList.forEach((elem) => {
        if (item.spuId == elem.spuId) {
          if (elem.count) {
            elem.count++;
          } else {
            elem.count = 1;
          }
        }
      });
      if (!flag) {
        item.count = 1;
        state.CartData.push(item);
      }
      state.totalPrice = 0;
      state.CartData.forEach((elem) => {
        state.totalPrice += elem.count * elem.originPrice;
        // console.log(state.totalPrice);
      });
      localStorage.setItem("MTCart", JSON.stringify(state.CartData));

      state.showListData = JSON.parse(JSON.stringify(state.showListData));
      // state.showListData.spuList=[...state.showListData.spuList]
    },
    Minus(state, item) {
      state.CartData.forEach((elem, index) => {
        if (item.spuId == elem.spuId) {
          elem.count--;
          if (elem.count == 0) {
            state.CartData.splice(index, 1);
          }
        }
      });
      state.showListData.spuList.forEach((elem) => {
        if (item.spuId == elem.spuId) {
          elem.count--;
          if (elem.count == 0) {
            elem.count = 0;
          }
        }
      });
      state.totalPrice = 0;
      state.CartData.forEach((elem) => {
        state.totalPrice += elem.count * elem.originPrice;
        // console.log(state.totalPrice);
      });
      localStorage.setItem("MTCart", JSON.stringify(state.CartData));
      state.showListData = JSON.parse(JSON.stringify(state.showListData));
    },
    Submit(state) {
      state.CartData = [];
      state.totalPrice = 0;
      localStorage.setItem("MTCart", JSON.stringify(state.CartData));
    },
  },
  actions: {
    GetData(context) {
      axios.get("api/category.json").then((resCategory) => {
        axios.get("api/listData.json").then((resListData) => {
          context.commit("GetData", {
            CategoryData: resCategory.data.data.kingkongList,
            ListData: resListData.data.data.shopList,
          });
        });
      });
    },
    DetailsId(context, id) {
      axios.get("api/shopdetails.json").then((resShopDetails) => {
        context.commit("DetailsId", {
          id: id,
          ShopDetailsData: resShopDetails.data,
        });
      });
    },
    SelectIndex(context, index) {
      context.commit("SelectIndex", index);
    },
    AddCart(context, item) {
      context.commit("AddCart", item);
    },
    Minus(context, item) {
      context.commit("Minus", item);
    },
    Submit(context) {
      context.commit("Submit");
    },
  },
  modules: {},
});
